package com.wyl.servlet

import com.wyl.pojo.Person
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 获取session和基本设置
 */
@WebServlet("/session01")
class SessionServlet01 : HttpServlet() {
    override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
        req.characterEncoding = "utf-8"
        resp.contentType = "text/html;charset=UTF-8"

        resp.writer.use { out ->
            req.session.apply {
                setAttribute("name", "Java")
                setAttribute("person", Person("xiaobai", 18))
                if (isNew) {
                    out.println("创建成功: $id")
                } else {
                    out.println("已经存在: $id")
                }
            }
        }
    }

    override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
        doPost(req, resp)
    }
}